﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Analysis.Business
{
    public class eMail
    {
        public bool Gmail(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(kime + "   Adlı kullanıcıya mail iletilemedi lütfen tekrar deneyin \r\n" + ex.Message);
                return false;
            }
        }
        public void Gmail2(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
            }
            catch (Exception)
            {
            }
        }
        public bool Smtp(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587");
                //objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true")
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(kime + "   Adlı kullanıcıya mail iletilemedi lütfen tekrar deneyin \r\n" + ex.Message);
                return false;
            }
        }

        public static void KullaniyorMailiGonder()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<div>" + DateTime.Now.ToLongDateString() + "</div>");
                sb.Append("<div>" + DateTime.Now.ToLongTimeString() + "</div>");
                sb.Append("<div>" + Environment.MachineName + "</div>");
                sb.Append("<div>" + Environment.OSVersion + "</div>");
                sb.Append("<div>" + Environment.UserDomainName + "</div>");
                sb.Append("<div>" + Environment.UserName + "</div>");
                sb.Append("<div>" + Environment.Version.ToString() + "</div>");
                sb.Append("<div>" + Config.GetConfig.GetValue(Parametre.Server) + "</div>");
                sb.Append("<div>" + Config.GetConfig.GetValue(Parametre.Db) + "</div>");
                new eMail().Gmail2("tr.freelancer@gmail.com", "tr.freelancer@gmail.com", "tr.freelancer@gmail.com", "kahraman", "Analiz Kullanılıyor", sb.ToString(), "smtp.gmail.com");
            }
            catch (Exception)
            {
            }
        }

    }
}
